<template>
  <div class="FromGood">
    <el-form
      :model="From"
      :rules="ruleGood"
      ref="From"
      label-width="120px"
      class="demo-ruleForm"
    >
      <el-form-item label="商品名称" prop="goodsName">
        <el-input disabled v-model="From.goodsName"></el-input>
      </el-form-item>
      <el-form-item label="商品描述" prop="goodsDescription">
        <el-input v-model="From.name" disabled></el-input>
      </el-form-item>
      <el-form-item label="商品类型" prop="goodsType">
        <el-select v-model="From.goodsType" disabled>
          <el-option label="其他(服务月卡会员)" value="1"></el-option>
          <el-option label="艾香" value="2"></el-option>
          <el-option label="艾浴包" value="3"></el-option>
          <el-option label="艾皂" value="4"></el-option>
          <el-option label="爱茶" value="5"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品备注" prop="goodsRemarkType">
        <el-select v-model="From.goodsRemarkType" disabled>
          <el-option
            label="普通商品"
            value="1"
            :disabled="From.goodsType == '1'"
          ></el-option>
          <el-option
            label="月卡"
            value="2"
            :disabled="From.goodsType != '1'"
          ></el-option>
          <el-option
            label="会员"
            value="3"
            :disabled="From.goodsType != '1'"
          ></el-option>
          <el-option
            label="服务"
            value="4"
            :disabled="From.goodsType != '1'"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        v-if="From.goodsRemarkType != 1"
        label="库存"
        prop="stockNum"
      >
        <el-input-number
          disabled
          v-model="From.stockNum"
          :step="1"
          :precision="0"
          :max="10000"
          :min="1"
        ></el-input-number>
      </el-form-item>
      <el-form-item
        v-if="From.goodsRemarkType != 1"
        label="商品有效期(天)"
        prop="goodsVaildPeriod"
      >
        <el-input-number
          disabled
          v-model="From.goodsVaildPeriod"
          :step="1"
          :precision="0"
          :max="10000"
          :min="1"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="成本价(元)" prop="costPrice">
        <el-input disabled v-model="From.costPrice" size=""></el-input>
      </el-form-item>
      <el-form-item label="售卖价(元)" prop="sellPrice">
        <el-input disabled v-model="From.sellPrice" size=""></el-input>
      </el-form-item>
      <el-form-item label="优惠价(元)" prop="discountPrice">
        <el-input disabled v-model="From.discountPrice" size=""></el-input>
      </el-form-item>
      <el-form-item label="商品状态" prop="goodsStatus">
        <el-select v-model="From.goodsStatus" disabled>
          <el-option label="缺货" value="0"></el-option>
          <el-option label="正常" value="1"></el-option>
          <el-option label="下线" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="进货数量(元)" prop="discountPrice">
        <el-input-number
          v-model="From.inStockNum"
          :step="1"
          :precision="0"
          :max="From.stockNum"
          :min="1"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="进货商店" prop="store">
        <el-select v-model="From.store">
          <el-option
            v-for="item in From.storeList"
            :key="item.id"
            :label="item.storeName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('From')"
          >确认订单</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: ["id"],
  data() {
    return {
      uploadbaseurl: null,
      From: {
        goodsName: null,
        goodsType: "1",
        goodsRemarkType: "2",
        goodsSellType: "1",
        goodsVaildPeriod: 1,
        goodsDescription: 1,
        costPrice: 1,
        sellPrice: 1,
        stockNum: 1,
        discountPrice: 1,
        goodsStatus: "0",
        store: "",
        inStockNum: 1,
      },
      fileList: [],
      ruleGood: {},
      dialogImageUrl: "",
      dialogVisible: false,
      storeList: "",
    };
  },
  computed: {
    newValue() {
      return this.From.goodsType;
    },
  },
  watch: {
    newValue(val, old) {
      if (val != 1) {
        this.From.goodsRemarkType = "1";
      } else if (val == 1) {
        this.From.goodsRemarkType = "2";
      }
    },
  },
  created() {
    this.initData();
    this.uploadbaseurl = this.baseurl;
  },
  methods: {
    async initData() {
      console.log(this.id);
      let res = await this.http.get("/goods/getById?id=" + this.id);
      console.log(res);
      if (!res.error) {
        this.From = !res.error ? res.result : {};
        this.From.goodsType = this.From.goodsType;
        this.From.goodsRemarkType = this.From.goodsRemarkType;
        this.From.goodsStatus = this.From.goodsStatus ? "1" : "0";

        console.log(this.fileList);
      } else
        this.eui.Message({
          message: `获取id为：'${this.id}'的商品信息失败`,
          type: "error",
        });
      console.log(this.From);
      let store = await this.http.get("/store/getList");
      this.storeList = store.error ? [] : store.result;
      this.From.storeList = this.storeList;
    },
    async submitForm() {
      console.log(this.From);
      let req = {
        goodsId: this.From.id,
        costPrice: this.From.costPrice,
        inStockNum: this.From.inStockNum,
        storeId: this.From.store,
      };

      if (!req.storeId) {
        this.eui.Message({
          message: "请选择商店",
          type: "error",
        });
        return false;
      }

      let res = await this.http.post("/goods/giveStoreGoods", req);
      if (!res.error)
        this.eui.Message({ message: "进货成功", type: "success" });
      else this.eui.Message({ message: "进货失败", type: "error" });
    },
  },
};
</script>

<style scoped>
.FromGood {
  width: 100%;
  min-height: 100%;
  padding: 15px;
  box-sizing: border-box;
}
</style>